package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.Companies;
import com.example.springboot.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/Enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @PostMapping("/add")
    public Result addCompany(@RequestBody Companies company) {
        System.out.println(company);
        enterpriseService.addCompany(company);
        return Result.success("添加成功");
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Companies> companyList = enterpriseService.findAll();
        return Result.success(companyList);
    }

    @PutMapping("/edit")
    public Result editCompany(@RequestBody Companies company) {
        enterpriseService.editCompany(company);
        return Result.success("编辑成功");
    }

    @DeleteMapping("/delete/{company_id}")
    public String deleteCompany(@PathVariable int company_id) {
        System.out.println(company_id);
        enterpriseService.deleteCompany(company_id);
        return "Company deleted successfully!";
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteCompanyBatch(@RequestBody List<Long> ids) {
        try {
            enterpriseService.deleteCompanyBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/selectByName")
    public Result selectByName(@RequestParam String company_name) {
        return Result.success(enterpriseService.selectByName(company_name));
    }
}